import GaoDeKey from '@/GaoDeMapKey';
import request from '@/request.js';
const GaoDeApi = 'https://restapi.amap.com/v3/config/district';

const methods = {
  handleClose() {
    for (let key in this.form) {
      this.form[key] = '';
    }
  },

  submit() {
    const form = this.form;
    form.userRequestServiceStartTime = new Date(
      form.userRequestServiceStartTime
    ).getTime();
    form.userRequestServiceEndTime = new Date(
      form.userRequestServiceEndTime
    ).getTime();
    form.predictServiceStartTime = new Date(
      form.predictServiceStartTime
    ).getTime();
    form.predictServiceEndTime = new Date(form.predictServiceEndTime).getTime();
    this.$emit('submit', form);
  },

  cancel() {
    this.ifDialog = false;
  },

  handleChooseCounty(county) {
    // 清空街道
    this.form.userCountry = '';
    const countries = this.chooseCityCounties.find(item => item.name === county)
      .districts;
    this.chooseCountyCountries = countries;
  },

  handleChooseCity(city) {
    // 清空 区，街道
    this.form.userCounty = '';
    this.form.userCountry = '';
    const counties = this.chooseProvinceCities.find(item => item.name === city)
      .districts;
    this.chooseCityCounties = counties;
  },

  async handleChooseProvince(province) {
    // 清空 市，区，街道
    this.form.userCity = '';
    this.form.userCounty = '';
    this.form.userCountry = '';
    try {
      const res = await request({
        url: GaoDeApi,
        method: 'get',
        params: {
          key: GaoDeKey,
          keywords: province,
          subdistrict: 3
        }
      });
      if (res && res.data && res.data.districts) {
        const district = res.data.districts[0];
        const cities = district.districts;
        this.chooseProvinceCities = cities;
      }
    } catch (err) {
      console.log(err);
    }
  }
};

export default methods;
